﻿namespace HySoft.Cash.Structure
{
    using System;
    using HiLand.Utility.Enums.OP;
    using System.Data;
    using HiLand.Utility.DataBase;

    [Serializable]
    public class TinvoicelistDT
    {
        public static TinvoicelistDT ConvertFrom(DataRow row)
        {
            TinvoicelistDT entity = new TinvoicelistDT();
            if (row != null)
            {
                entity.Sid = Convert.ToInt32(row["Sid"]);
                entity.OrgSid = Convert.ToInt32(row["OrgSid"]);
                entity.InvoiceDate = Convert.ToDateTime(row["InvoiceDate"]);

                if (DataRowHelper.IsExistField(row, "InvoiceAmount") && Convert.IsDBNull(row["InvoiceAmount"]) == false)
                {
                    entity.InvoiceAmount = Convert.ToDecimal(row["InvoiceAmount"]);
                }

                if (DataRowHelper.IsExistField(row, "IsValid") && Convert.IsDBNull(row["IsValid"]) == false)
                {
                    entity.IsValid = (InvoiceStatuses)Convert.ToInt32(row["IsValid"]);
                }

                if (DataRowHelper.IsExistField(row, "ReceiveAmount") && Convert.IsDBNull(row["ReceiveAmount"]) == false)
                {
                    entity.ReceiveAmount = Convert.ToDecimal(row["ReceiveAmount"]);
                }

                if (DataRowHelper.IsExistField(row, "Note") && Convert.IsDBNull(row["Note"]) == false)
                {
                    entity.Note = Convert.ToString(row["Note"]);
                }
                if (DataRowHelper.IsExistField(row, "Penalty") && Convert.IsDBNull(row["Penalty"]) == false)
                {
                    entity.Penalty = Convert.ToDecimal(row["Penalty"]);
                }
                if (DataRowHelper.IsExistField(row, "LateInterestCharges") && Convert.IsDBNull(row["LateInterestCharges"]) == false)
                {
                    entity.LateInterestCharges = Convert.ToDecimal(row["LateInterestCharges"]);
                }
                if (DataRowHelper.IsExistField(row, "Principal") && Convert.IsDBNull(row["Principal"]) == false)
                {
                    entity.Principal = Convert.ToDecimal(row["Principal"]);
                }
                if (DataRowHelper.IsExistField(row, "Interest") && Convert.IsDBNull(row["Interest"]) == false)
                {
                    entity.Interest = Convert.ToDecimal(row["Interest"]);
                }
                if (DataRowHelper.IsExistField(row, "InterestPaid") && Convert.IsDBNull(row["InterestPaid"]) == false)
                {
                    entity.InterestPaid = Convert.ToDecimal(row["InterestPaid"]);
                }
                if (DataRowHelper.IsExistField(row, "LateInterestChargesPaid") && Convert.IsDBNull(row["LateInterestChargesPaid"]) == false)
                {
                    entity.LateInterestChargesPaid = Convert.ToDecimal(row["LateInterestChargesPaid"]);
                }
                if (DataRowHelper.IsExistField(row, "PenaltyPaid") && Convert.IsDBNull(row["PenaltyPaid"]) == false)
                {
                    entity.PenaltyPaid = Convert.ToDecimal(row["PenaltyPaid"]);
                }
                if (DataRowHelper.IsExistField(row, "PrincipalPaid") && Convert.IsDBNull(row["PrincipalPaid"]) == false)
                {
                    entity.PrincipalPaid = Convert.ToDecimal(row["PrincipalPaid"]);
                }
                if (DataRowHelper.IsExistField(row, "PaymentDate") && Convert.IsDBNull(row["PaymentDate"]) == false)
                {
                   
                        entity.PaymentDate = Convert.ToDateTime(row["PaymentDate"]);
                    
                }
                if (DataRowHelper.IsExistField(row, "Guid") && Convert.IsDBNull(row["Guid"]) == false)
                {
                    entity.Guid = Convert.ToString(row["Guid"]);
                }
                if (DataRowHelper.IsExistField(row, "SendStatus") && Convert.IsDBNull(row["SendStatus"]) == false)
                {
                    entity.SendStatus = (InvoiceSendStatuses)Convert.ToInt32(row["SendStatus"]);
                }

                if (DataRowHelper.IsExistField(row, "LastCreditBalance") && Convert.IsDBNull(row["LastCreditBalance"]) == false)
                {
                    entity.LastCreditBalance = Convert.ToDecimal(row["LastCreditBalance"]);
                }

                if (DataRowHelper.IsExistField(row, "LastFeeBalance") && Convert.IsDBNull(row["LastFeeBalance"]) == false)
                {
                    entity.LastFeeBalance = Convert.ToDecimal(row["LastFeeBalance"]);
                }

                if (DataRowHelper.IsExistField(row, "PreviousBalance") && Convert.IsDBNull(row["PreviousBalance"]) == false)
                {
                    entity.PreviousBalance = Convert.ToDecimal(row["PreviousBalance"]);
                }

                if (DataRowHelper.IsExistField(row, "PaidFromPreviousBalance") && Convert.IsDBNull(row["PaidFromPreviousBalance"]) == false)
                {
                    entity.PaidFromPreviousBalance = Convert.ToDecimal(row["PaidFromPreviousBalance"]);
                }

                if (DataRowHelper.IsExistField(row, "PreviousInvoiceID") && Convert.IsDBNull(row["PreviousInvoiceID"]) == false)
                {
                    entity.PreviousInvoiceID = Convert.ToInt32(row["PreviousInvoiceID"]);
                }

                if (DataRowHelper.IsExistField(row, "OrgName") && Convert.IsDBNull(row["OrgName"]) == false)
                {
                    entity.OrgName = Convert.ToString(row["OrgName"]);
                }

                if (DataRowHelper.IsExistField(row, "PreferredEmail") && Convert.IsDBNull(row["PreferredEmail"]) == false)
                {
                    entity.PreferredEmail = Convert.ToString(row["PreferredEmail"]);
                }
            }

            return entity;
        }

        public static TinvoicelistDT ConvertFrom(IDataReader reader)
        {
            TinvoicelistDT entity = new TinvoicelistDT();
            if (reader != null)
            {
                entity.Sid = Convert.ToInt32(reader["Sid"]);
                entity.OrgSid = Convert.ToInt32(reader["OrgSid"]);
                entity.InvoiceDate = Convert.ToDateTime(reader["InvoiceDate"]);
                if (DataReaderHelper.IsExistField(reader, "InvoiceAmount"))
                {
                    entity.InvoiceAmount = Convert.ToDecimal(reader["InvoiceAmount"]);
                }
                entity.IsValid = (InvoiceStatuses)Convert.ToInt32(reader["IsValid"]);
                if (DataReaderHelper.IsExistField(reader, "ReceiveAmount"))
                {
                    entity.ReceiveAmount = Convert.ToDecimal(reader["ReceiveAmount"]);
                }
                entity.Note = Convert.ToString(reader["Note"]);
                entity.Penalty = Convert.ToDecimal(reader["Penalty"]);
                entity.LateInterestCharges = Convert.ToDecimal(reader["LateInterestCharges"]);
                entity.Principal = Convert.ToDecimal(reader["Principal"]);
                entity.Interest = Convert.ToDecimal(reader["Interest"]);
                entity.InterestPaid = Convert.ToDecimal(reader["InterestPaid"]);
                entity.LateInterestChargesPaid = Convert.ToDecimal(reader["LateInterestChargesPaid"]);
                entity.PenaltyPaid = Convert.ToDecimal(reader["PenaltyPaid"]);
                entity.PrincipalPaid = Convert.ToDecimal(reader["PrincipalPaid"]);
                if (Convert.IsDBNull(reader["PaymentDate"]) == false)
                {
                    entity.PaymentDate = Convert.ToDateTime(reader["PaymentDate"]);
                }
                entity.Guid = Convert.ToString(reader["Guid"]);
                entity.SendStatus = (InvoiceSendStatuses)Convert.ToInt32(reader["SendStatus"]);

                if (DataReaderHelper.IsExistField(reader, "LastCreditBalance") && Convert.IsDBNull(reader["LastCreditBalance"]) == false)
                {
                    entity.LastCreditBalance = Convert.ToDecimal(reader["LastCreditBalance"]);
                }

                if (DataReaderHelper.IsExistField(reader, "LastFeeBalance") && Convert.IsDBNull(reader["LastFeeBalance"]) == false)
                {
                    entity.LastFeeBalance = Convert.ToDecimal(reader["LastFeeBalance"]);
                }

                if (DataReaderHelper.IsExistField(reader, "PreviousBalance") && Convert.IsDBNull(reader["PreviousBalance"]) == false)
                {
                    entity.PreviousBalance = Convert.ToDecimal(reader["PreviousBalance"]);
                }

                if (DataReaderHelper.IsExistField(reader, "PaidFromPreviousBalance") && Convert.IsDBNull(reader["PaidFromPreviousBalance"]) == false)
                {
                    entity.PaidFromPreviousBalance = Convert.ToDecimal(reader["PaidFromPreviousBalance"]);
                }

                if (DataReaderHelper.IsExistField(reader, "PreviousInvoiceID") && Convert.IsDBNull(reader["PreviousInvoiceID"]) == false)
                {
                    entity.PreviousInvoiceID = Convert.ToInt32(reader["PreviousInvoiceID"]);
                }


                if (DataReaderHelper.IsExistField(reader, "OrgName") && Convert.IsDBNull(reader["OrgName"]) == false)
                {
                    entity.OrgName = Convert.ToString(reader["OrgName"]);
                }

                if (DataReaderHelper.IsExistField(reader, "PreferredEmail") && Convert.IsDBNull(reader["PreferredEmail"]) == false)
                {
                    entity.PreferredEmail = Convert.ToString(reader["PreferredEmail"]);
                }

            }

            return entity;
        }


        public static string allFields = "Sid,OrgSid,InvoiceDate,Principal , Interest,(Principal + Interest+ Penalty+ LateInterestCharges ) as InvoiceAmount,IsValid,PrincipalPaid, InterestPaid,PenaltyPaid,LateInterestChargesPaid,(PrincipalPaid + InterestPaid + PenaltyPaid + LateInterestChargesPaid) as ReceiveAmount,Note,Penalty,LateInterestCharges,PaymentDate,Guid,SendStatus,LastCreditBalance,LastFeeBalance,PreviousBalance,PaidFromPreviousBalance,PreviousInvoiceID";
        public string Guid;

        public DateTime InvoiceDate;
        public InvoiceStatuses IsValid;
        public string Note;
        public int OrgSid;
        public DateTime PaymentDate;

        public int Sid;
        public InvoiceSendStatuses SendStatus;

        public decimal Principal;
        public decimal Interest;
        public decimal Penalty;
        public decimal LateInterestCharges;
        public decimal InvoiceAmount;

        public decimal PrincipalPaid;
        public decimal InterestPaid;
        public decimal PenaltyPaid;
        public decimal LateInterestChargesPaid;
        public decimal ReceiveAmount;

        public decimal LastCreditBalance;
        public decimal LastFeeBalance;

        public decimal PreviousBalance;
        public decimal PaidFromPreviousBalance;

        public int PreviousInvoiceID;

        public string OrgName = string.Empty;
        public string PreferredEmail = string.Empty;
    }

    public enum InvoiceStatuses
    {
        [EnumItemDescription("Display", "New")]
        InitStatus = 1, //新生成的账单
        [EnumItemDescription("Display", "Deleted")]
        Deleted = 3,//表示删除
        //[EnumItemDescription("Display", "Sent")]
        //Sent = 5,//账单已经发送
        [EnumItemDescription("Display", "Overdue")]
        Overdue = 6,//逾期（即还有Balance的情况）
        [EnumItemDescription("Display", "Closed")]
        Closed = 8,//账款还完（即Balance为0的情况下）
    }

    public enum InvoiceSendStatuses
    {
        NeedSend = 0,//需要发送
        NeedReSend = 10,//需要重新发送
        Sent = 20,//已发送
    }
}

